<?php
class ShopAction extends Action{

	public function getCategory(){//【获取所有分类及其子分类】
		$shop_cate = M('Shop_category');
		//获取以及分类
		$data = $shop_cate->where("status = 1 and pid = 0")->order("sort asc")->select();
		foreach ($data as $key=>$pid){
			$pid = $pid['shop_category_id'];
			$sub = $shop_cate->where("pid = $pid and status = 1")->order("sort asc")->select();
			$data[$key]['subcategory'] = $sub?$sub:array();
			
		}
		
		if($data){
			$value = ["code" =>"success",'data'=>$data];
			header("Content-Type: text/json");
			die(base64_encode(json_encode($value)));
		}else{
			
			if(!mysql_error($data)){
				$value = ["code" =>"success",'data'=>array()];
				header("Content-Type: text/json");
				die(base64_encode(json_encode($value)));
			}else{
				$value = ["code" =>"false"];
				header("Content-Type: text/json");
				die(base64_encode(json_encode($value)));
			
			}
		
		}
	
	}
	
	public function getProducts(){//【获取分类对应产品列表】
		$cid = $_REQUEST['cid'];
		$order = $_REQUEST['order']=='1'?'sold':'pid';//排序方式，默认pid降序，1则为销量降序
		
		$shop_cate = M('Shop_category');
		$shop_product = M('Shop_product');
		$now = time();
		$find = $shop_cate->where("shop_category_id = $cid and status = 1")->find();
		if(!$find){//不存在或已关闭
			$value = ["code" =>"false"];
			header("Content-Type: text/json");
			die(base64_encode(json_encode($value)));
		}
		
		if(!$find){//已关闭
			$value = ["code" =>"false"];
			header("Content-Type: text/json");
			die(base64_encode(json_encode($value)));
		}
		if($this->is_topCategory($cid)){//一级分类
			$sub = $shop_cate->where("pid = $cid and status = 1")->order("sort asc")->field("shop_category_id as cid")->select();
			$sub_array = array();
			foreach ($sub as $s){
				$sub_array[] = $s['cid'];
			}
		
			$sub_array = implode(',',$sub_array);
			
			$data = $shop_product->where("shop_category_id in($sub_array) and endtime > $now")->order("$order desc,pid desc")->select();
			
		}else{//二级分类
			
			$data = $shop_product->where("shop_category_id = $cid and endtime > $now")->order("$order desc,pid desc")->select();
			
		}
		if($data){
			foreach ($data as $key=>$da){
				$logo = D('attach')->where('attach_id='.$da['product_ico'])->find();
				$data[$key]['product_ico'] = UPLOAD_URL.'/'.$logo['save_path'].$logo['save_name'];
			}
		}else{
			$data = array();
		}
		
		$value = ["code" =>"success",'data'=>$data];
		header("Content-Type: text/json");
		die(base64_encode(json_encode($value)));
	
	}
	
	public function getProduct(){//【获取单个产品信息】
		$pid = $_REQUEST['pid'];
		$shop_product = M('Shop_product');
		$now = time();
		$data = $shop_product->where("pid = $pid and endtime > $now")->select();
		if($data){
			foreach ($data as $key=>$da){
				$logo = D('attach')->where('attach_id='.$da['product_ico'])->find();
				$data[$key]['product_ico'] = UPLOAD_URL.'/'.$logo['save_path'].$logo['save_name'];
			}
			$value = ["code" =>"success",'data'=>$data];
			header("Content-Type: text/json");
			die(base64_encode(json_encode($value)));
		}else{
			$value = ["code" =>"false"];
			header("Content-Type: text/json");
			die(base64_encode(json_encode($value)));
		}
	
	}
	public function is_topCategory($cid){//【判断是否为一级分类】
		$shop_cate = M('Shop_category');
		if($shop_cate->where("pid = $cid")->find()){
			return true;
		}else{
			return false;
		}
	
	}

/**-------------------------------------关于用户购买---------------------------------------------------**/

	public function addOrder(){//【添加订单】
		$uid = $_REQUEST['uid'];
		$pid = $_REQUEST['pid'];
		$qty = $_REQUEST['qty']?$_REQUEST['qty']:1;
		
		$name = $_REQUEST['name'];
		$phone = $_REQUEST['phone'];
		$address = $_REQUEST['address'];
		$zip_code = $_REQUEST['zip_code'];
		
		if(empty($uid) || empty($pid) || empty($name) || empty($phone) || empty($address) || empty($zip_code)){//参数不能为空
			$value = ["code" =>"empty"];
			header("Content-Type: text/json");
			die(base64_encode(json_encode($value)));
		
		}
		
		$shop_product = M('Shop_product');
		$credit_user = M('Credit_user');
		$shop_order = M('Shop_order');
		$shop_order_data = M('Shop_order_data');
		//获取当前用户所有积分
		$credit_all =$this->getCredit($uid);
		//获取产品需要的总积分
		$time = time();
		$product_credit = $shop_product->where("pid = $pid and endtime > $time")->find();
		if(!$product_credit){//产品不存在或已关闭
			$value = ["code" =>"productfalse"];
			header("Content-Type: text/json");
			die(base64_encode(json_encode($value)));
		
		}
		$product_credit = $product_credit['credit']*$qty;
		
	
		if($product_credit > $credit_all){//积分不够
			$value = ["code" =>"creditless"];
			header("Content-Type: text/json");
			die(base64_encode(json_encode($value)));
		}
		
		//检查库存
		$product_info = $shop_product->where("pid = $pid and endtime > $time")->find();
		//dump($product_credit);exit;
		$product_qty = $product_info['product_num'];
		if($product_qty < $qty){//库存不足
			$value = ["code" =>"qtyless"];
			header("Content-Type: text/json");
			die(base64_encode(json_encode($value)));
		
		}
		
		$shop_order->uid = $uid;
		$shop_order->pid = $pid;
		$shop_order->qty = $qty;
		$shop_order->credit_consume = $product_credit;
		$shop_order->ctime = time();
		//dump($shop_order);exit;
		$oid  =$shop_order->add();
		if($oid){
			$shop_order_data->oid = $oid;
			$shop_order_data->name = $name;
			$shop_order_data->phone = $phone;
			$shop_order_data->address = $address;
			$shop_order_data->zip_code = $zip_code;
			$add_data = $shop_order_data->add();
			if($add_data){
				//扣除积分
				$credit_user->score = $credit_all-$product_credit;
				$credit_user->where("uid = $uid")->save();
				//减去库存
				$shop_product->product_num = $product_qty-$qty;
				$shop_product->save();
				$data = $this->getOrderdata($oid);
				
				$value = ["code" =>"success",'data'=>$data];
				header("Content-Type: text/json");
				die(base64_encode(json_encode($value)));
			}else{
				$shop_order->where("oid = $oid")->delete();
				$value = ["code" =>"false"];
				header("Content-Type: text/json");
				die(base64_encode(json_encode($value)));
			}
		}else{
			$value = ["code" =>"false"];
			header("Content-Type: text/json");
			die(base64_encode(json_encode($value)));
		}
		
	}
	
	public function getCredit($uid){//【获取积分】
		$credit_user = M('Credit_user');
		$get_credit = $credit_user->where("uid = $uid")->field("score")->find();
		if($get_credit){
			return $get_credit['score'];
		}else{
			return '0';
		}
		}
	public function getOrderdata($oid){
		$shop_order = M('Shop_order');
		$shop_order_data = M('Shop_order_data');
		$data = $shop_order->table("wz_shop_order O")->join("wz_shop_order_data D on O.oid = D.oid")->where("O.oid = $oid")->field("O.*,D.*")->find();
		//获取产品信息
		if($data){
			$shop_product = M('Shop_product');
			$info = $shop_product->where('pid='.$data['pid'])->find();
			$logo = D('attach')->where('attach_id='.$info['product_ico'])->find();
			$data['product_name'] = $info['product_name'];
			$data['product_ico'] = UPLOAD_URL.'/'.$logo['save_path'].$logo['save_name'];
		}
		return $data;
	}

	public function getOrder(){//【获取订单】
		$map = array();
		$_REQUEST['uid'] && $map['O.uid'] = $_REQUEST['uid'];
		$_REQUEST['oid'] && $map['O.oid'] = $_REQUEST['oid'];
		if(empty($map)){
			$value = ["code" =>"empty"];
			header("Content-Type: text/json");
			die(base64_encode(json_encode($value)));
		}
		$shop_order = M('Shop_order');
		$shop_order_data = M('Shop_order_data');
		$data = $shop_order->table("wz_shop_order O")->join("wz_shop_order_data D on O.oid = D.oid")->where($map)->order("ctime desc")->field("O.*,D.*")->select();
		if($data){
			$shop_product = M('Shop_product');
			foreach ($data as $key=>$da){
				$info = $shop_product->where('pid='.$da['pid'])->find();
				$logo = D('attach')->where('attach_id='.$info['product_ico'])->find();
				$data[$key]['product_name'] = $info['product_name'];
				$data[$key]['product_ico'] = UPLOAD_URL.'/'.$logo['save_path'].$logo['save_name'];
			}
			$value = ["code" =>"success",'data'=>$data];
			header("Content-Type: text/json");
			die(base64_encode(json_encode($value)));
		}else{
			if(!mysql_error($data)){
				$value = ["code" =>"success",'data'=>array()];
				header("Content-Type: text/json");
				die(base64_encode(json_encode($value)));
			}else{
				$value = ["code" =>"false"];
				header("Content-Type: text/json");
				die(base64_encode(json_encode($value)));
			
			}
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	


}